package com.pmsystem.controller;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pmsystem.pojo.News;
import com.pmsystem.service.NewsService;

/**
 * 
* <p>Title: NewsController</p>  
* <p>Description:新闻政务发布的控制器 </p>  
* @author 树昌  
* @date 2019年1月24日
 */
@Controller
@RequestMapping("office/")
public class NewsController {

	@Autowired
	NewsService newsService;
	
	/**
	 * 
	 * <p>Title: writeNews</p>  
	 * <p>Description:编写新闻草稿的控制器 </p>  
	 * @author 树昌
	 * @param news
	 * @return 跳转到编辑者的页面
	 */
	@RequestMapping(value="write",method=RequestMethod.POST)
	@ResponseBody
	public Map<String ,String > writeNews(News news) {
		Map<String ,String > map =new HashMap<>();
		String msg=null;
		boolean row = newsService.writeNews(news);
		if(row) {
			msg= "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: showEditorNews</p>  
	 * <p>Description:编辑者查看新闻视图的控制器 </p>  
	 * @author 树昌
	 * @param keyword 搜索关键词
	 * @param startRow	页码
	 * @param pageSize	一页显示的记录条数
	 * @return 不做跳转
	 */
	@RequestMapping(value="editor",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> showEditorNews(String keyword, Integer startRow, Integer pageSize,Date startTime,Date endTime){
		
		Map<String, Object> map = new HashMap<>();
		List<News> listnews =  newsService.showEditorNews(keyword, startRow, pageSize,startTime,endTime);
		map.put("listnews", listnews);
		int size = listnews.size();
		map.put("size", size);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: releaseNews</p>  
	 * <p>Description:发布新闻 </p>  
	 * @author 树昌
	 * @param news
	 * @return	跳转到阅读者页面
	 */
	@ResponseBody
	@RequestMapping(value="releasenews",method=RequestMethod.POST)
	public Map<String, Object> releaseNews(int id) {
	
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		News news = new News();
		news.setNewsId(id);
		boolean row = newsService.releaseNews(news);
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	
	/**
	 * 
	 * <p>Title: deleteNews</p>  
	 * <p>Description:删除新闻 </p>  
	 * @author 树昌
	 * @param news
	 * @return 不做跳转
	 */
	@ResponseBody
	@RequestMapping(value="deletenews",method=RequestMethod.POST)
	public Map<String, Object> deleteNews(int id) {
		
		
		Map<String, Object> map = new HashMap<>();
		String msg=null;
		News news = new News();
		news.setNewsId(id);
		boolean row = newsService.deleteNews(news);
		if(row==true) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: deleteNews</p>  
	 * <p>Description:批量删除新闻 </p>  
	 * @author 树昌
	 * @param news
	 * @return 不做跳转
	 */
	@ResponseBody
	@RequestMapping(value="deleteAllnews",method=RequestMethod.POST)
	public Map<String, Object> deleteAllNews(int [] id) {
		Map<String, Object> map = new HashMap<>();
		String msg=null;
		List<Integer> listID = new ArrayList<>();
		for (int i : id) {
			listID.add(i);
		}
		
		boolean row = newsService.deleteAllNews(listID);
		
		if(row) {
			msg = "success";
		}else {
			msg = "error";
		}
		map.put("msg", msg);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: updateNews</p>  
	 * <p>Description:修改一条新闻的控制器 </p>  
	 * @author 树昌
	 * @param news
	 * @return 跳转到编辑者页面
	 */
	@ResponseBody
	@RequestMapping(value="updatenews",method=RequestMethod.POST)
	public Map<String, Object> updateNews(News news) {
		
		Map<String, Object> map = new HashMap<>();
		String msg = null;
		boolean row = newsService.updateNews(news);
		
		if(row) {
			msg = "success";
		}else {
			msg =  "error";
		}
		
		map.put("msg", msg);
		return map;
	}
	
	/**
	 * 
	 * <p>Title: showNewsByKeyID</p>  
	 * <p>Description:根据主键查询一条新闻记录 </p>  
	 * @author 树昌
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="showNewsByKey",method=RequestMethod.POST)
	public Map<String, Object> showNewsByKeyID(int id){
		
		Map<String, Object> map = new HashMap<>();
		News news = newsService.showOneNews(id);
		map.put("news", news);
		return map;
	}
	
}
